//
//  RZLatestMessageInfo.m
//  RZIM
//
//  Created by reyzhang on 2021/8/2.
//

#import "RZMessageInfo.h"
#import "RZIMClient.h"
#import <MJExtension/MJExtension.h>
#import <SDWebImage/SDWebImage.h>


/**
 @property (nonatomic, strong) NSString *gid;
 @property (nonatomic, strong) NSString *mid;
 @property (nonatomic, strong) NSString *msg;
 @property (nonatomic, strong) NSString *msgTime;

 @property (nonatomic, assign) NSInteger msgState;
 @property (nonatomic, assign) NSInteger msgType;
 @property (nonatomic, assign) NSInteger offline;
 @property (nonatomic, assign) NSInteger subType;
 @property (nonatomic, assign) NSInteger tid;
 @property (nonatomic, assign) NSInteger uid;
 */
@implementation RZMessageInfo

/**
 socket消息 转换
 */
+ (instancetype)createWithSocketMessage:(RZMessage *)message {
    RZMessageInfo *msgInfo = [RZMessageInfo new];
    
    msgInfo.gid = message.gid;
    msgInfo.mid = message.message.msgId;
    msgInfo.msg = message.message.msg;
    msgInfo.msgTime = message.message.msgTime;
    
    msgInfo.msgType = 1; //大分类，系统消息，聊天消息
    
    msgInfo.subType = message.message.msgType; //这个是消息类型
    
    msgInfo.tid = message.tid;
    msgInfo.uid = message.uid;
    msgInfo.username = message.fromUser.username;
    msgInfo.userAvatar = message.fromUser.userAvatar;
    
    msgInfo.extraData = message.message.extraData;
    id info = msgInfo.extraInfo; //会调用getter方法
    NSLog(@"extra info :%@",info);
    return msgInfo;
    
}


#pragma mark GETTER

- (RZMessageDirection)messageDirection {
    NSString *curId = [[RZIMClient sharedClient] userInfo].uid;
    return [curId isEqualToString:self.uid] ? MessageDirection_SEND : MessageDirection_RECEIVE;
}

- (id)extraInfo {
    if (self.extraData == nil || self.extraData == [NSNull null]) {
        return nil;
    }
    
    NSDictionary *dic;
    //如果是字符串类型，转成json对象
    if ([self.extraData isKindOfClass:[NSString class]]) {
        NSData *data = [self.extraData dataUsingEncoding:NSUTF8StringEncoding];
        dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves|NSJSONReadingAllowFragments error:nil];
    }else {
        dic = self.extraData;
    }
    
    if (self.subType == MessageType_AUDIO) { // 语音消息的 扩展信息
        RZVoiceMessageModel *model = [RZVoiceMessageModel mj_objectWithKeyValues:dic];
        return model;
    }else if (self.subType == MessageType_PICTURE) { //图片消息的 扩展信息
        RZImageMessageModel *model = [RZImageMessageModel mj_objectWithKeyValues:dic];
        UIImageView *imageV = [[UIImageView alloc] init];
        [imageV sd_setImageWithURL:[NSURL URLWithString:self.msg] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            model.thumbImage = image;
        }];
        return model;
    }else if (self.subType == MessageType_VIDEO) { //视频消息
        RZVideoMessageModel *model = [RZVideoMessageModel mj_objectWithKeyValues:dic];
        UIImageView *imageV = [[UIImageView alloc] init];
        [imageV sd_setImageWithURL:[NSURL URLWithString:model.imageUrl] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            model.thumbImage = image;
        }];
        return model;
    }
    
    return dic;
}

@end


@implementation RZVoiceMessageModel
@end

@implementation RZImageMessageModel
@end

@implementation RZVideoMessageModel
@end
